# 指定编译器
CXX := /usr/bin/g++
TARGET := liblogger.so
INCLUDE_DIR := ../include
LIB_DIR := ../lib

# 查找所有的.cpp文件
SOURCES := $(wildcard *.cpp)
# 生成对应的.o文件名
OBJECTS := $(SOURCES:.cpp=.o)

# 编译和链接选项
CXXFLAGS := -Wall -std=c++17 -lpthread -I$(INCLUDE_DIR)
LIBFLAGS := -fPIC -shared

# 默认目标
all: $(TARGET)

# 链接目标
$(TARGET): $(OBJECTS)
	@echo "Linking $@..."
	@$(CXX) $^ -o $@  $(LIBFLAGS) || (echo "Error linking $@"; exit 1)

# 编译规则
%.o: %.cpp
	@echo "Compiling $<..."
	@$(CXX) -c $< -o $@ $(CXXFLAGS) $(LIBFLAGS) || (echo "Error compiling $<"; exit 1)

# 清理目标
.PHONY: clean install
clean:
	@echo "Cleaning up..."
	@rm -f *.o $(TARGET)

# 安装目标
install: $(TARGET)
	@echo "Installing $(TARGET) to $(LIB_DIR)..."
	@mv $(TARGET) $(LIB_DIR)/ || (echo "Error installing $(TARGET)"; exit 1)
	@echo "Updating LD_LIBRARY_PATH..."
	@export LD_LIBRARY_PATH=$(LIB_DIR):$$LD_LIBRARY_PATH || (echo "Error updating LD_LIBRARY_PATH"; exit 1)